import multiprocessing
import threading
import time
import os
# from concurrent.futures import ThreadPoolExecutor
# from concurrent.futures import ProcessPoolExecutor


def create_file(file_name):
    content = '一般情况下，主进程会等待子进程全部结束后，才会结束。但是，很多时候，当主进程结束后，子进程会立即结束，' \
              '比如：关闭浏览器（Google)，打开的网页会直接结束这个时候需要将守护主进程daemon设置为True'
    for i in range(50):
        file_root = r'D:\Users\asus-pc\Desktop\a'
        if not os.path.exists(file_root):
            os.mkdir(file_root)
        file_path_finally = file_root + os.sep + str(file_name) + str(i) + '.txt'
        # print(file_path_finally)
        if not os.path.exists(file_path_finally):
            with open(file_path_finally, 'w', encoding='utf8') as f:
                f.write(content)
        else:
            print('文件已存在')


def copy_file(filename, source_dir, target_dir):
    # 拼接文件路径
    source_path = source_dir + os.sep + filename
    target_path = target_dir + os.sep + filename
    # 打开原文件，并获取其中的内容, 然后打开拷贝文件，并写入内容
    print('正在拷贝到', target_path)
    with open(source_path, 'r', encoding='utf8') as source_file:
        with open(target_path, 'w', encoding='utf8') as target_file:
            while True:
                data = source_file.read(1024)
                if data:
                    target_file.write(data)
                else:
                    break
    # time.sleep(0.2)


if __name__ == '__main__':
    """
    小文件，单线程>多线程>多进程
    大文件，多进程>多线程>单线程
    使用线程池应该会更快，但是进程池不能计时，
    只能通过flask的方式计时，也不知道是不是正确的
    """
    # # 创建文件
    # file_list = [x for x in range(10)]
    # with ProcessPoolExecutor() as pool:
    #     pool.map(create_file, file_list)
    # 文件路径
    source_dir = r'D:\Users\asus-pc\Desktop\a'
    single_dir = r'D:\Users\asus-pc\Desktop\single'
    multi_process_dir = r'D:\Users\asus-pc\Desktop\multi_process'
    multi_thread_dir = r'D:\Users\asus-pc\Desktop\multi_thread'

    # 获取需要拷贝文件的文件名
    file_list = os.listdir(source_dir)
    # 判断拷贝文件路径是否存在，不存在则创建
    if not os.path.exists(single_dir):
        os.mkdir(single_dir)
    if not os.path.exists(multi_process_dir):
        os.mkdir(multi_process_dir)
    if not os.path.exists(multi_thread_dir):
        os.mkdir(multi_thread_dir)

    # 单线程
    start_time = time.time()
    for file_name in file_list:
        copy_file(file_name, source_dir, single_dir)
    end_time = time.time()
    single_during_time = end_time - start_time

    # 多进程
    start_time = time.time()
    for file_name in file_list:
        multi_thread_copy_file = multiprocessing.Process(target=copy_file,
                                                         args=(file_name, source_dir, multi_process_dir))
        multi_thread_copy_file.start()
    # multi_process_pool = ProcessPoolExecutor()
    # for file_name in file_list:
    #     multi_process_pool.submit(copy_file, file_name, source_dir, multi_process_dir)
    end_time = time.time()
    multi_process_during_time = end_time - start_time

    # # 多线程
    start_time = time.time()
    for file_name in file_list:
        multi_thread_copy_file = threading.Thread(target=copy_file, args=(file_name, source_dir, multi_thread_dir))
        multi_thread_copy_file.start()
    # multi_thread_pool = ThreadPoolExecutor()
    # for file_name in file_list:
    #     multi_thread_pool.submit(copy_file, file_name, source_dir, multi_thread_dir)
    end_time = time.time()
    multi_thread_during_time = end_time - start_time
    time.sleep(0.5)
    print('single during time:', single_during_time)
    print('multi_process during time:', multi_process_during_time)
    print('multi_thread during time:', multi_thread_during_time)
